@echo off
setlocal EnableDelayedExpansion

:: --- UTF-8 & FARBE ---
chcp 65001 >nul
color 0B
cls
title K-L-U-K Copy Cat - Mapped Drive Edition (Administrator)


:: --- ANSI FARBEN AKTIVIEREN ---
for /F %%a in ('echo prompt $E ^| cmd') do set "ESC=%%a"

:: --- SCHNELLE VERSION-ERFASSUNG ---
for /f "tokens=2*" %%a in ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v DisplayVersion 2^>nul') do set "WIN_VER=%%b"
title K-L-U-K Copy Cat - Windows %WIN_VER% 

:: --- ADMIN-CHECK ---
net session >nul 2>&1
if %errorLevel% neq 0 (
    echo [*] Kopiere CopyCat lokal fuer stabilen Admin-Start...
    copy /Y "%~dpnx0" "%TEMP%\CopyCat_Temp.bat" >nul
    echo [!] Fordere Admin-Rechte an...
    :: Brechstange: Wir zwingen Windows, das moderne Terminal (wt.exe) als Admin zu nutzen!
    powershell -NoProfile -Command "$bat = '%TEMP%\CopyCat_Temp.bat'; if (Get-Command 'wt.exe' -ErrorAction SilentlyContinue) { Start-Process 'wt.exe' -ArgumentList \"cmd.exe  /c `\"$bat`\"\" -Verb RunAs } else { Start-Process $bat -Verb RunAs }"
    exit
)
:: --- FENSTERGROESSE SETZEN (Modern + Legacy Fallback) ---
:: Versucht zuerst das moderne Windows Terminal per ANSI-Code zu resizen (Hoehe=65, Breite=60)
echo %ESC%[8;65;60t
:: Fallback fuer das alte DOS-Fenster, falls wt.exe nicht aktiv ist
mode con: cols=65 lines=60 >nul 2>&1
:: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- ::
:: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- ::
:: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- ::
:MENU
cls
color 0B
cls
echo.
echo.
echo __________________________________________v.202606
echo.
echo        K-L-U-K Copy Cat - The Agency Edition
echo		     aktuelles Windows = %WIN_VER%
echo __________________________________________________
echo.
echo [0] SYSTEM-CHECK (Details)
echo --------------------------------------------------
echo [1] VON NAS nach C:\Temp (LOKAL AUSROLLEN)
echo --------------------------------------------------
echo [2] MasterInstall.ps1 (Die Volle Installation!)
echo [3] ENERGIEOPTIONEN only (No Sleep Mode)
echo --------------------------------------------------
echo [4] Desktop SAEUBERN und Spotlight Kill
echo [5] C:\Temp und appdata-TEMP komplett LOESCHEN
echo [6] windows.old, inetpub, perflogs LOESCHEN
echo [7] BLOATWARE vernichten (Junk-Entfernung)
echo [8] TASKBAR rebuild und Explorer@DIESER PC
echo --------------------------------------------------
echo [U] USER MANAGEMENT
echo --------------------------------------------------
echo [T] TeamViewer Zuweisung 
echo --------------------------------------------------
echo [O] OUTLOOK Profil-Migration (Export/Import)
echo --------------------------------------------------
echo [R] SYSTEM NEUSTARTEN (*)
echo [Q] Beenden
echo --------------------------------------------------
echo %ESC%[31m-ACHTUNG: B idR NICHT nutzen. Nur fuer Entwicklung-%ESC%[0m
echo %ESC%[31m[B] VON DEVelopment-System nach NAS (BACKUP) (*)%ESC%[0m
echo.
echo -- (*) = GROSSSCHREIBUNG WICHTIG --
echo.
set /p "AUSWAHL=choose your weapon!? (0/...5/U/R/Q/B): "
color 0B
if "%AUSWAHL%"=="0" goto SYS_CHECK
if "%AUSWAHL%"=="1" goto LOCAL
if "%AUSWAHL%"=="2" goto INSTALL
if "%AUSWAHL%"=="3" goto ENERGY
if "%AUSWAHL%"=="4" goto DESKTOP_CLEAN
if "%AUSWAHL%"=="5" goto CLEANUP
if "%AUSWAHL%"=="6" goto CLEANUP_OLD
if "%AUSWAHL%"=="7" goto BLOAT_CLEAN
if "%AUSWAHL%"=="8" goto TASKBAR_CLEAN
if /i "%AUSWAHL%"=="U" goto USER_MGT
if /i "%AUSWAHL%"=="T" goto TV_ASSIGN
if /i "%AUSWAHL%"=="O" goto OUTLOOK_MIGRATE
if "%AUSWAHL%"=="R" goto REBOOT
if "%AUSWAHL%"=="B" goto BACKUP
if /i "%AUSWAHL%"=="Q" exit
goto MENU



:: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- ::
:: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- ::
:: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- ::
:: ==================================================
:: HAUPTMEN  AKTIONEN
:: ==================================================
:SYS_CHECK
cls
echo [*] Sammle Systemdaten... (Hardware-Abfrage laeuft)
set "PS_CMD=$os=Get-CimInstance Win32_OperatingSystem; $cpu=Get-CimInstance Win32_Processor; $mb=Get-CimInstance Win32_BaseBoard; $disk=Get-PhysicalDisk | Select-Object -First 1; $tpm=Get-Tpm; $sb=Confirm-SecureBootUEFI; $ram=[math]::round((Get-CimInstance Win32_ComputerSystem).TotalPhysicalMemory / 1GB, 2); $vol=Get-PSDrive C; $free=[math]::round($vol.Free/1GB,2); $total=[math]::round($vol.Used/1GB + $free, 2); $lic=Get-CimInstance SoftwareLicensingProduct | Where-Object {$_.ApplicationID -eq '55c92734-d682-4d71-983e-d6ec3f16059f'}; $act=if($lic.LicenseStatus -eq 1){'Aktiviert'}else{'Nicht aktiviert'}; $key=(Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SoftwareProtectionPlatform').BackupProductKeyDefault; Write-Host '--- SYSTEM INFOS ---'; Write-Host 'Produkt-Key        :' $key; Write-Host 'Windows Edition    :' $os.Caption; Write-Host 'Aktivierungsstatus :' $act; Write-Host 'CPU                :' $cpu.Name; Write-Host 'RAM                :' $ram 'GB'; Write-Host 'Mainboard          :' $mb.Manufacturer $mb.Product; Write-Host 'Festplatte (Typ)   :' $disk.MediaType; Write-Host 'TPM Vorhanden      :' $tpm.TpmPresent; Write-Host 'Secure Boot        :' $sb; Write-Host 'Speicher C:        :' $free 'GB frei von' $total 'GB'"
powershell -NoProfile -Command "%PS_CMD%"
pause
goto MENU

:LOCAL
cls
echo [*] Bereinige Laufwerk Z: und verbinde neu...
:: Erst sicherstellen, dass Z: frei ist (auch wenn es vorher schon mal offen war)
net use z: /delete /y >nul 2>&1

:: Jetzt mit Admin-Rechten frisch verbinden
net use Z: "\\Nas\backup\0 skript" /user:install 1234 /persistent:no
if %errorlevel% neq 0 (
    echo [!] Fehler: NAS-Verbindung fehlgeschlagen!
    pause
    goto MENU
)

echo [*] Kopiere vom NAS...
:: Jetzt kann robocopy auf Z: zugreifen, da es im Admin-Kontext gemappt wurde
robocopy "Z:" "C:\Temp" /MIR /R:3 /W:5 /XD 25h2 backup test installer *.reg

echo [*] Erstelle Faulenzer-Verknuepfungen auf dem Desktop...
powershell -NoProfile -Command "$wshell = New-Object -ComObject WScript.Shell; $s1 = $wshell.CreateShortcut(\"$env:USERPROFILE\Desktop\TEMP Ordner.lnk\"); $s1.TargetPath = 'C:\Temp'; $s1.Save(); $s2 = $wshell.CreateShortcut(\"$env:USERPROFILE\Desktop\CopyCat.lnk\"); $s2.TargetPath = 'C:\Temp\00-KLUK-CopyCat-v1.bat'; $s2.WorkingDirectory = 'C:\Temp'; $s2.Save()"

echo [OK] Zeug ist lokal und Verknuepfungen liegen bereit.
echo [*] Trenne NAS und springe zur lokalen CopyCat ueber...

:: Z: sauber abraeumen
net use Z: /delete /y >nul 2>&1

:: Die lokale Version in einem neuen Fenster starten (Arbeitsverzeichnis direkt auf C:\Temp setzen)
start /D "C:\Temp" "" "C:\Temp\00-KLUK-CopyCat-v1.bat"

:: Alte NAS-Version gnadenlos beenden
exit
pause

:INSTALL
cls
echo [*] Suche nach MasterInstall.ps1...
set "NAS_PATH=\\Nas\backup\0 skript\MasterInstall.ps1"

:: 1. Suche lokal
if exist "C:\Temp\MasterInstall.ps1" (
    set "INST_PATH=C:\Temp\MasterInstall.ps1"
) else if exist "%~dp0MasterInstall.ps1" (
    set "INST_PATH=%~dp0MasterInstall.ps1"
) else (
    :: 2. Wenn nicht lokal, NAS-Verbindung erzwingen
    echo [*] NAS-Verbindung fuer Suche sicherstellen...
    net use "\\Nas\backup" /delete /y >nul 2>&1
    net use "\\Nas\backup" "1234" /USER:"install" /PERSISTENT:NO >nul 2>&1
    
    :: 3. Jetzt nochmal pr fen
    if exist "%NAS_PATH%" (
        set "INST_PATH=%NAS_PATH%"
    ) else (
        echo [!] Fehler: MasterInstall.ps1 nirgends gefunden!
        pause
        goto MENU
    )
)

echo [*] Zuende %INST_PATH%...
powershell -NoProfile -ExecutionPolicy Bypass -File "%INST_PATH%"
pause
goto MENU

:ENERGY
cls
echo [*] Setze Energieoptionen auf "No Sleep"...
powercfg /change standby-timeout-ac 0
powercfg /change standby-timeout-dc 0
powercfg /setacvalueindex scheme_current sub_buttons lidaction 0
powercfg /setdcvalueindex scheme_current sub_buttons lidaction 0
powercfg /setactive scheme_current
echo [OK] Energieoptionen gesetzt. Die Kiste schlaeft nicht mehr ein!
pause
goto MENU

:CLEANUP
cls
echo [*] Vernichte C:\Temp...
if exist "C:\Temp" (
    rmdir /S /Q "C:\Temp" >nul 2>&1
    echo [OK] C:\Temp geloescht.
)

echo [*] Vernichte Appdata Temp...
if exist "C:\Temp" (
    rmdir /S /Q "C:\Users\%USERNAME%\AppData\Local\Temp" >nul 2>&1
    echo [OK] Appdata Temp geloescht.
)

echo [OK] User-Temp und Appdata-Temp wurde bereinigt.
pause
goto MENU

:DESKTOP_CLEAN
cls
echo [!] Desktop-Saeuberung und Spotlight-Kill (Systemweit)...
set "PATH1=%USERPROFILE%\Desktop"
set "PATH2=C:\Users\default\Desktop"
set "PATH3=%PUBLIC%\Desktop"

:: 1. Desktops putzen
for %%P in ("%PATH1%" "%PATH2%" "%PATH3%") do (
    if exist "%%~P" (
        powershell -Command "Get-ChildItem -Path '%%~P' -File | Remove-Item -Force -ErrorAction SilentlyContinue"
    )
)

:: 2. Spotlight Kill 
winget uninstall --id Microsoft.WindowsWebExperiencePack --silent --accept-source-agreements --force >nul 2>&1
winget uninstall --id Microsoft.BingNews --silent --accept-source-agreements --force >nul 2>&1
winget uninstall --id Windows-Spotlight.Windows-Spotlight --silent --accept-source-agreements --force >nul 2>&1

:: 3. Windows zwingen, das sofort anzuwenden
RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters
echo [OK] Desktops geputzt, Spotlight gekillt und fuer alle User dauerhaft blockiert.
pause
goto MENU

:CLEANUP_OLD
cls
echo [*] Bereinige System-Altlasten (Chemische Reinigung)...

:: 1. Dienste stoppen, die die Ordner blockieren koennten
echo [*] Stoppe blockierende Dienste...
taskkill /f /im explorer.exe >nul 2>&1
net stop TrustedInstaller >nul 2>&1
net stop WSearch >nul 2>&1
net stop w3svc >nul 2>&1

:: 2. Besitzrechte mit Gewalt uebernehmen und Ordner vaporisieren
for %%D in ("C:\Windows.old" "C:\inetpub" "C:\PerfLogs") do (
    if exist "%%~D" (
        echo [*] Vernichte: %%~D
        takeown /f "%%~D" /r /d y >nul 2>&1
        icacls "%%~D" /grant administrators:F /t /c /q >nul 2>&1
        attrib -r -a -s -h "%%~D" /s /d >nul 2>&1
        rd /s /q "%%~D"
    )
)

:: 4. Dienste wieder starten und Explorer zurueckholen
net start WSearch >nul 2>&1
start explorer.exe
echo [OK] Windows.old, inetpub und perflogs sind Geschichte.
pause
goto MENU

:: 4. Dienste wieder starten und Explorer zurueckholen
net start WSearch >nul 2>&1
start explorer.exe
echo [OK] Windows.old ist weg.
pause
goto MENU

:REBOOT
cls
echo [*] Achtung: Neustart in 5 Sekunden...
shutdown /r /t 5
exit

:BACKUP
cls
echo [*] Kopiere zum NAS (Backup)...
robocopy "C:\Temp" "\\Nas\backup\0 skript" /MIR /R:3 /W:5
pause
goto MENU

:TV_ASSIGN
cls
echo [*] Pruefe TeamViewer...

:: 1. NAS Anmeldung im Admin-Kontext erzwingen
net use "\\Nas\backup" "1234" /user:"install" >nul 2>&1

:: 2. PowerShell Logik mit lokalem Cache-Check
powershell -NoProfile -ExecutionPolicy Bypass -Command ^
    "$tvPath = 'C:\Program Files\TeamViewer\TeamViewer.exe'; " ^
    "$nas = '\\Nas\backup\0 skript\teamviewer\TeamViewer_Host_Setup.exe'; " ^
    "$tempDir = 'C:\Temp\teamviewer'; " ^
    "$tempExe = 'C:\Temp\teamviewer\TeamViewer_Host_Setup.exe'; " ^
    "if (Test-Path $tvPath) { " ^
    "  Write-Host '[OK] TeamViewer ist schon installiert. Spar dir die Muehe.' -ForegroundColor Green; " ^
    "} else { " ^
    "  if (-not (Test-Path $tempDir)) { New-Item -ItemType Directory -Force -Path $tempDir | Out-Null } " ^
    "  if (Test-Path $tempExe) { " ^
    "    Write-Host 'Lokaler Cache gefunden! Spare Bandbreite, starte direkt...' -ForegroundColor Cyan; " ^
    "    Start-Process -FilePath $tempExe -ArgumentList '/S --grant-easy-access --minimize' -Wait; " ^
    "    Write-Host '[OK] Installation aus dem lokalen Vorrat beendet.' -ForegroundColor Green; " ^
    "  } elseif (Test-Path $nas) { " ^
    "    Write-Host 'NAS online. Sauge Installer nach C:\Temp\teamviewer...' -ForegroundColor Cyan; " ^
    "    Copy-Item -Path $nas -Destination $tempExe -Force; " ^
    "    Write-Host 'Starte Installation...' -ForegroundColor Cyan; " ^
    "    Start-Process -FilePath $tempExe -ArgumentList '/S --grant-easy-access --minimize' -Wait; " ^
    "    Write-Host '[OK] Installation durch, wische Spuren weg.' -ForegroundColor Green; " ^
    "    Remove-Item -Path $tempExe -Force -ErrorAction SilentlyContinue; " ^
    "  } else { " ^
    "    Write-Host '[!] Fehler: Weder lokaler Cache noch NAS erreichbar! NAS-Zugang am Arsch?' -ForegroundColor Red; " ^
    "  } " ^
    "}"

:: 3. NAS Verbindung wieder trennen
net use "\\Nas\backup" /delete >nul 2>&1

pause
goto MENU

:BLOAT_CLEAN
cls
echo [*] Vernichte Bloatware und raeume Startmenue-Leichen weg...
powershell -NoProfile -Command ^
    "$bloat = @('Microsoft.Getstarted', 'Microsoft.ZuneMusic', 'Microsoft.ZuneVideo', 'Microsoft.BingWeather', 'Microsoft.XboxApp', 'Microsoft.Messaging', 'Microsoft.People', 'Microsoft.WindowsMaps', 'Microsoft.MicrosoftSolitaireCollection', 'Microsoft.BingNews', 'Microsoft.Office.OneNote', 'Microsoft.SkypeApp'); " ^
    "foreach ($app in $bloat) { " ^
    "  Get-AppxPackage -AllUsers | Where-Object { $_.Name -like \"*$app*\" } | Remove-AppxPackage -AllUsers -ErrorAction SilentlyContinue; " ^
    "  Get-AppxProvisionedPackage -Online | Where-Object { $_.PackageName -like \"*$app*\" } | Remove-AppxProvisionedPackage -Online -ErrorAction SilentlyContinue; " ^
    "} ; " ^
    "Write-Host 'Bereinige Startmenue-Datenbanken...'; " ^
    "Get-Process 'StartMenuExperienceHost' -ErrorAction SilentlyContinue | Stop-Process -Force; " ^
    "Remove-Item -Path \"$env:LOCALAPPDATA\Packages\Microsoft.Windows.StartMenuExperienceHost_*\TempState\*\" -Recurse -Force -ErrorAction SilentlyContinue; " ^
    "Write-Host '[OK] Junk vernichtet.'"
	
	:: Widgets & Empfehlungen im Startmen  komplett killen
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v "Start_IrisRecommendations" /t REG_DWORD /d 0 /f >nul

pause
goto MENU

:TASKBAR_CLEAN
cls
echo [*] Taskbar-Modus: Linksbuendig, Suche weg und Explorer auf 'Dieser PC'...
echo [*] Explorer-Neustart folgt...

:: 1. Taskbar nach links (0=Links, 1=Zentriert)
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v "TaskbarAl" /t REG_DWORD /d 0 /f >nul 2>&1

:: 2. Suche ausblenden (0=Versteckt, 1=Icon, 2=Suche+Icon, 3=Suche+Text)
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Search" /v "SearchboxTaskbarMode" /t REG_DWORD /d 0 /f >nul 2>&1

:: 3. Explorer direkt auf 'Dieser PC' oeffnen (1=Dieser PC, 2=Schnellzugriff)
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v "LaunchTo" /t REG_DWORD /d 1 /f >nul 2>&1

:: --- Default User Impfung fuer neue Profile ---
if exist "C:\Users\Default\NTUSER.DAT" (
    echo [*] Passe Einstellungen fuer zukuenftige Profile an...
    reg load HKU\DefaultTemplate "C:\Users\Default\NTUSER.DAT" >nul 2>&1
    reg add "HKU\DefaultTemplate\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v "TaskbarAl" /t REG_DWORD /d 0 /f >nul 2>&1
    reg add "HKU\DefaultTemplate\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v "LaunchTo" /t REG_DWORD /d 1 /f >nul 2>&1
    reg unload HKU\DefaultTemplate >nul 2>&1
)

echo [*] Exorzismus: Entferne Windows Web Experience Pack...
powershell -NoProfile -Command ^
    "Get-AppxPackage -AllUsers *WebExperience* | Remove-AppxPackage -AllUsers -ErrorAction SilentlyContinue; " ^
    "winget uninstall --id Microsoft.WindowsWebExperiencePack --silent --accept-source-agreements --force 2> $null"
	
:: 4. Explorer neu starten
taskkill /f /im explorer.exe >nul 2>&1
start explorer.exe

echo [OK] Taskbar ist links, Suche ist weg und Explorer oeffnet in 'Dieser PC'.
pause
goto MENU



:: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- ::
:: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- ::
:: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- ::
:: ==================================================
:: Outlook Profil migration
:: ==================================================
:: ==================================================
:: ==================================================
:OUTLOOK_MIGRATE
cls
echo [!] Outlook Backup und Migration...
echo [*] ACHTUNG: Outlook muss vorher komplett geschlossen sein!
echo.
echo [E] Profil Exportieren
echo [I] Profil Importieren
echo [S] Signaturen / Cache (Backup/Restore)
echo [X] DEINSTALL Outlook.Crap.App
echo [Q] Zurueck zum Hauptmenue
echo.
set /p "AUSWAHL=Waehle: "
if /i "%AUSWAHL%"=="E" goto O_EXPORT
if /i "%AUSWAHL%"=="I" goto O_IMPORT
if /i "%AUSWAHL%"=="S" goto O_SIGS
if /i "%AUSWAHL%"=="X" goto O_KILL
if /i "%AUSWAHL%"=="Q" goto MENU
goto OUTLOOK_MIGRATE

:O_EXPORT
set "REGFILE=\\nas\backup\0 skript\backup\outlook_profile.reg"

:: Erst pruefen, ob die Datei existiert
if exist "%REGFILE%" (
    echo [!] Datei existiert bereits auf dem NAS!
    set "OVERWRITE="
    set /p "OVERWRITE=Soll die Datei ueberschrieben werden? (J/N): "
    
    :: Hier erzwingen wir die Abfrage
    if /i "!OVERWRITE!" neq "J" (
        echo [*] Abbruch. Keine Aenderung vorgenommen.
    pause
	echo.
	echo.
	goto OUTLOOK_MIGRATE
    )
)

echo [*] Exportiere Outlook-Profile zum NAS...
reg export "HKCU\Software\Microsoft\Office\16.0\Outlook\Profiles" "%REGFILE%" /y
echo [OK] Export fertig.
pause & goto OUTLOOK_MIGRATE

:O_IMPORT
set "REGFILE=\\nas\backup\0 skript\backup\outlook_profile.reg"
if not exist "%REGFILE%" (
    echo [!] Fehler: Kein Backup auf dem NAS gefunden!
    pause & goto OUTLOOK_MIGRATE
)

echo [!] ACHTUNG: Das lokale Outlook-Profil wird mit dem Backup ueberschrieben.
set /p "CONFIRM=Wirklich importieren? (J/N): "
if /i "%CONFIRM%" neq "J" (
    echo [*] Import abgebrochen.
    pause
	echo.
	echo.
	goto OUTLOOK_MIGRATE
)

echo [*] Importiere Outlook-Profile vom NAS...
reg import "%REGFILE%" >nul
reg add "HKCU\Software\Microsoft\Office\16.0\Outlook" /v "DefaultProfile" /t REG_SZ /d "Outlook" /f >nul
echo [OK] Import fertig.
pause & goto OUTLOOK_MIGRATE

:O_SIGS
cls
echo [S] Signatur und Cache Migration:
echo [1] Backup zum NAS
echo [2] Restore vom NAS
echo [Q] Abbrechen
set /p "SIG_CHOICE=Waehle (1/2/Q): "
set "NAS_BASE=\\nas\backup\0 skript\backup\OutlookData"
set "LOCAL_SIG=%AppData%\Microsoft\Signatures"
set "LOCAL_CACHE=%LocalAppData%\Microsoft\Outlook\RoamCache"

if "%SIG_CHOICE%"=="1" (
    if exist "%NAS_BASE%\Signatures" (
        set /p "OW=Ueberschreiben? (J/N): "
        if /i "!OW!" neq "J" goto O_SIGS
    )
    mkdir "%NAS_BASE%\Signatures" 2>nul
    mkdir "%NAS_BASE%\RoamCache" 2>nul
    robocopy "%LOCAL_SIG%" "%NAS_BASE%\Signatures" /MIR /R:0 /W:0 >nul
    robocopy "%LOCAL_CACHE%" "%NAS_BASE%\RoamCache" Stream_Autocomplete*.dat /R:0 /W:0 >nul
    echo [OK] Sicherung beendet.
)
if "%SIG_CHOICE%"=="2" (
    set /p "OW=Lokale Daten wirklich ueberschreiben? (J/N): "
    if /i "!OW!"=="J" (
        robocopy "%NAS_BASE%\Signatures" "%LOCAL_SIG%" /MIR /R:0 /W:0 >nul
        robocopy "%NAS_BASE%\RoamCache" "%LOCAL_CACHE%" Stream_Autocomplete*.dat /R:0 /W:0 >nul
        echo [OK] Restore beendet.
    )
)
pause & goto OUTLOOK_MIGRATE

:O_KILL
echo [*] Vernichte Neues Outlook...
reg add "HKCU\Software\Microsoft\Office\16.0\Outlook\Options\General" /v "HideNewOutlookToggle" /t REG_DWORD /d 1 /f >nul 2>&1
powershell -NoProfile -Command "Get-AppxPackage -AllUsers *Microsoft.OutlookForWindows* | Remove-AppxPackage -AllUsers -ErrorAction SilentlyContinue"
echo [OK] Web-Outlook ist Geschichte.
pause & goto OUTLOOK_MIGRATE

:: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- ::
:: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- ::
:: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- ::
:: ==================================================
:: USER MANAGEMENT SUB-MEN 
:: ==================================================
:USER_MGT
cls
echo __________________________________________________
echo.
echo               USER MANAGEMENT 
echo __________________________________________________
echo.
echo [1] SUPPORT User (EDV.Agency) anlegen/verwalten
echo [2] NEUEN USER anlegen
echo [3] JORDSAND Vorlage (Quick-Setup)
echo [4] User verwalten (User: Liste und Loeschen)
echo.
echo [B] Zurueck zum Hauptmenue
echo __________________________________________________
echo.
set /p "UMENU=Was darfs sein? (1/2/3/4/B): "

if "%UMENU%"=="1" goto USR_SUPPORT
if "%UMENU%"=="2" goto USR_NEW
if "%UMENU%"=="3" goto USR_JORDSAND
if "%UMENU%"=="4" goto USR_LIST_DELETE
if /i "%UMENU%"=="B" goto MENU
goto USER_MGT

:: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- ::
:USR_LIST_DELETE
cls

echo [*] User-Verwaltung - Liste, Aendern und Loeschen
echo --------------------------------------------------
echo Username ^| Status ^| Sichtbar ^| Aktiv
echo --------------------------------------------------
for /f "skip=4 tokens=1,2,3" %%a in ('net user') do (
    for %%u in (%%a %%b %%c) do (
        if not "%%u"=="" if not "%%u"=="Der" if not "%%u"=="Befehl" if not "%%u"=="wurde" (
            set "IS_ADMIN=User"
            set "COLOR_CODE=%ESC%[37m"
            net localgroup Administratoren | find /i "%%u" >nul 2>&1
            if !errorlevel! equ 0 ( set "IS_ADMIN=ADMIN" & set "COLOR_CODE=%ESC%[33m" )
            
            set "VIS=Sichtbar"
            reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList" /v %%u >nul 2>&1
            if !errorlevel! equ 0 set "VIS=NINJA"

            set "AKTIV=JA"
            for /f %%g in ('powershell -NoProfile -Command "(Get-LocalUser -Name '%%u').Enabled" 2^>nul') do set "USER_ENABLED=%%g"
            if /i "!USER_ENABLED!"=="False" ( set "AKTIV=NEIN" & set "COLOR_CODE=%ESC%[31m" )
            
            echo !COLOR_CODE!%%u : !IS_ADMIN! : !VIS! : !AKTIV!%ESC%[0m
        )
    )
)
echo --------------------------------------------------
echo [A] ADMIN / [U] USER 
echo [N] unsichtbarer NINJA / [S] SICHTBAR
echo [E] ENABLE (Aktivieren) / [D] DISABLE (Deaktivieren)
echo [L] LOESCHEN
echo [B] ZURUECK
echo --------------------------------------------------
set /p "LISTACTION=Aktion waehlen: "

:: Neue Sprungmarken f r Enable/Disable
if /i "%LISTACTION%"=="E" goto USR_MOD_ENABLE
if /i "%LISTACTION%"=="D" goto USR_MOD_DISABLE
if /i "%LISTACTION%"=="A" goto USR_MOD_ADMIN
if /i "%LISTACTION%"=="U" goto USR_MOD_USER
if /i "%LISTACTION%"=="N" goto USR_MOD_HIDE
if /i "%LISTACTION%"=="S" goto USR_MOD_SHOW
if /i "%LISTACTION%"=="L" goto USR_EXEC_DELETE
if /i "%LISTACTION%"=="B" goto USER_MGT
goto USR_LIST_DELETE

:USR_MOD_ENABLE
set /p "DNAME=Wer soll wieder aktiv werden? "
net user "%DNAME%" /active:yes
goto USR_LIST_DELETE

:USR_MOD_DISABLE
set /p "DNAME=Wer soll deaktiviert werden? "
net user "%DNAME%" /active:no
goto USR_LIST_DELETE

:USR_MOD_ADMIN
set /p "DNAME=Welchen User zum Admin befoerdern? "
powershell -Command "Add-LocalGroupMember -Group (Get-LocalGroup | ? SID -match 'S-1-5-32-544').Name -Member '%DNAME%' -ErrorAction SilentlyContinue"
echo [OK] %DNAME% ist jetzt Admin.
pause 
goto USR_LIST_DELETE

:USR_MOD_USER
set /p "DNAME=Welchen Admin degradieren? "
powershell -Command "Remove-LocalGroupMember -Group (Get-LocalGroup | ? SID -match 'S-1-5-32-544').Name -Member '%DNAME%' -ErrorAction SilentlyContinue"
echo [OK] %DNAME% ist jetzt Standard-User.
pause 
goto USR_LIST_DELETE

:USR_MOD_HIDE
set /p "DNAME=Wer soll in den Ninja-Mode? "
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList" /v %DNAME% /t REG_DWORD /d 0 /f >nul 2>&1
echo [OK] %DNAME% ist jetzt unsichtbar.
pause 
goto USR_LIST_DELETE

:USR_MOD_SHOW
set /p "DNAME=Wer soll wieder sichtbar werden? "
reg delete "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList" /v %DNAME% /f >nul 2>&1
echo [OK] %DNAME% ist wieder sichtbar.
pause 
goto USR_LIST_DELETE

:USR_EXEC_DELETE
set /p "DNAME=User zum Loeschen: "
echo [!] WIRKLICH LOESCHEN?
set /p "CONFIRM=(J/N): "
if /i "%CONFIRM%"=="J" (
    :: Benutzer loeschen
    net user "%DNAME%" /delete
    
    :: Ordner loeschen Abfrage
    if exist "C:\Users\%DNAME%" (
        echo [*] Benutzerordner "C:\Users\%DNAME%" gefunden.
        set /p "DELFOLDER=Ordner auch entfernen? (J/N): "
        if /i "!DELFOLDER!"=="J" (
            echo [*] Loesche Benutzerordner...
            :: Explorer kurz stoppen, falls er den Ordner noch lockt
            taskkill /f /im explorer.exe >nul 2>&1
            rd /s /q "C:\Users\%DNAME%"
            start explorer.exe
            echo [OK] Ordner gel scht.
        )
    ) else (
        echo [-] Kein Benutzerordner unter C:\Users\%DNAME% gefunden.
    )
)
pause
goto USR_LIST_DELETE

:: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- ::

:USR_SUPPORT
cls
echo [*] Pruefe auf 'support'...
net user support >nul 2>&1
if %errorlevel% neq 0 goto USR_SUPPORT_NEW

:: --- Support existiert: Status-Check ---
echo [+] User 'support' existiert!
reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList" /v support >nul 2>&1
if %errorlevel% equ 0 ( 
    echo [+] Status: Aktuell UNSICHTBAR
    set "AKT_STATUS=2"
) else ( 
    echo [+] Status: Aktuell SICHTBAR
    set "AKT_STATUS=1"
)

echo [*] Zwinge Admin-Rechte auf...
powershell -Command "Add-LocalGroupMember -Group (Get-LocalGroup | ? SID -match 'S-1-5-32-544').Name -Member 'support' -ErrorAction SilentlyContinue"

echo.
echo Was soll mit dem User passieren?
echo [0] Namenskorrektur in EDV.Agency
echo [1] Kennwort aendern
echo [2] Sichtbarkeit aendern (aktuell: %AKT_STATUS%)
echo [3] Alles beim Alten lassen (Exit)
set /p "SPASS=Auswahl: "

:: Logik: Wenn nicht 1 oder 2 gewaehlt wurde, sofort zurueck zum Menue
if "%SPASS%"=="0" goto USR_SUPPORT_RENAME
if "%SPASS%"=="1" goto USR_SUPPORT_PASS
if "%SPASS%"=="2" goto USR_SUPPORT_HIDE
goto USER_MGT

:USR_SUPPORT_RENAME
net user support /fullname:"EDV.Agency" /comment:"EDV.Agency" 
echo [UMBENANNT]
pause
goto USER_MGT

:USR_SUPPORT_PASS
set /p "NPASS=Neues Kennwort: "
net user support "!NPASS!"
echo [OK] Kennwort gesetzt.
pause
goto USER_MGT

:USR_SUPPORT_HIDE
echo Aktueller Status: %AKT_STATUS% (1=Sichtbar, 2=Unsichtbar)
set /p "SHIDE=Neu auf [1] Sichtbar oder [2] Unsichtbar setzen? "
if "%SHIDE%"=="2" (
    reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList" /v support /t REG_DWORD /d 0 /f >nul 2>&1
    echo [OK] User ist jetzt UNSICHTBAR.
) else (
    reg delete "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList" /v support /f >nul 2>&1
    echo [OK] User ist jetzt SICHTBAR.
)
pause
goto USER_MGT

:: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- ::

:USR_SUPPORT_NEW
:: --- Support existiert NICHT ---
echo [-] User 'support' fehlt. Wird erschaffen...
set /p "SADMIN=Als Admin anlegen? (J/N): "
set /p "SHIDE=Sichtbarkeit? [1] Sichtbar | [2] Unsichtbar: "
set /p "SPASS=Kennwort? [1] 1337 | [2] Frei ausgedacht: "

if "%SPASS%"=="1" set "UPASS=1337"
if "%SPASS%"=="2" set /p "UPASS=Bitte Kennwort eingeben: "

net user support "%UPASS%" /add /fullname:"EDV.Agency" /comment:"EDV.Agency" 

:: NEU: PowerShell statt wmic f r "Kennwort l uft nie ab"
powershell -NoProfile -Command "Set-LocalUser -Name 'support' -PasswordNeverExpires $true -ErrorAction SilentlyContinue"

if /i "%SADMIN%"=="J" (
    powershell -Command "Add-LocalGroupMember -Group (Get-LocalGroup | ? SID -match 'S-1-5-32-544').Name -Member 'support' -ErrorAction SilentlyContinue"
)

if "%SHIDE%"=="2" (
    reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList" /v support /t REG_DWORD /d 0 /f >nul 2>&1
) else (
    reg delete "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList" /v support /f >nul 2>&1
)

echo [OK] Support-User auf die Platte genagelt!
pause
goto USER_MGT

:: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- ::

:USR_NEW
cls
echo [*] Frische User fuer die EDV.Agency...
set /p "NNAME=Username: "
if "%NNAME%"=="" goto USER_MGT

:: NEU: Abfrage fuer Vollst ndigen Namen
set /p "NFULLNAME=Vollstaendiger Name: "
if "%NFULLNAME%"=="" set "NFULLNAME=%NNAME%"

set /p "NDESC=Beschreibung (Leer = Username): "
if "%NDESC%"=="" set "NDESC=%NNAME%"

echo.
echo Kennwort-Optionen:
echo [1] Kein Kennwort
echo [2] Username als Initialkennwort
echo [3] Frei ausgedacht
set /p "NPWOPT=Auswahl (1/2/3): "

if "%NPWOPT%"=="1" set "NPASS="
if "%NPWOPT%"=="2" set "NPASS=%NNAME%"
if "%NPWOPT%"=="3" set /p "NPASS=Kennwort eingeben: "

echo.
set /p "NISADMIN=Soll der User Admin sein? (J/N): "
set /p "NCHANGE=Muss Kennwort beim Erstlogin geaendert werden? (J/N): "

:: User anlegen mit /fullname Parameter
if "%NPWOPT%"=="1" (
    net user "%NNAME%" /add /fullname:"%NFULLNAME%" /comment:"%NDESC%"
) else (
    net user "%NNAME%" "%NPASS%" /add /fullname:"%NFULLNAME%" /comment:"%NDESC%"
)

:: PowerShell fuer Passwort-Policy
powershell -NoProfile -Command "Set-LocalUser -Name '%NNAME%' -PasswordNeverExpires $true -ErrorAction SilentlyContinue"

:: Admin Rechte
if /i "%NISADMIN%"=="J" (
    powershell -Command "Add-LocalGroupMember -Group (Get-LocalGroup | ? SID -match 'S-1-5-32-544').Name -Member '%NNAME%' -ErrorAction SilentlyContinue"
)

:: Kennwortaenderung
if /i "%NCHANGE%"=="J" (
    net user "%NNAME%" /logonpasswordchg:yes
) else (
    net user "%NNAME%" /logonpasswordchg:no
)

echo [OK] User %NNAME% (%NFULLNAME%) angelegt!
pause
goto USER_MGT

:: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- :: --- ::

:USR_JORDSAND
cls
echo [*] Initialisiere JORDSAND Benutzer & Setup...

:: 1. Benutzerverwaltung (Anlegen oder  berschreiben)
echo [*] Bereite Benutzer 'jordsand' vor...
net user jordsand jordsand /add /fullname:"Jordsand" /comment:"Jordsand" >nul 2>&1
:: Falls er schon existiert, wird das Passwort hiermit neu gesetzt ( berschreiben)
net user jordsand jordsand >nul 2>&1
:: Zum Admin machen
net localgroup Administratoren jordsand /add >nul 2>&1

:: Spotlight & Personalization Kill
echo [*] Setze Hintergrund auf 'Bild' (Spotlight aus)...
powershell -Command "if (!(Test-Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Personalization')) { New-Item -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Personalization' -Force | Out-Null }; Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Personalization' -Name 'LockScreenImageStatus' -Value 0 -ErrorAction SilentlyContinue"

echo [*] Spotlight-Manager Registry-Kill...
powershell -Command "$reg = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\ContentDeliveryManager'; if (!(Test-Path $reg)) { New-Item -Path $reg -Force | Out-Null }; Set-ItemProperty -Path $reg -Name 'RotatingLockScreenEnabled' -Value 0 -ErrorAction SilentlyContinue"


:: 2. NAS-Authentifizierung
echo [*] Authentifiziere gegen NAS...
net use \\192.168.178.2\backup /delete /y >nul 2>&1
net use \\192.168.178.2\backup 1234 /USER:install /PERSISTENT:NO >nul 2>&1

:: 3. Dynamische Pfade
set "SRC=\\192.168.178.2\backup\0 skript\bilder\wpp-jordsand-lq.jpg"
set "TARGET_DIR=C:\Users\Public\Pictures\jordsand"
set "DST=%TARGET_DIR%\jordsand-bg.jpg"

:: Achtung: Hier nutzen wir den festen User "jordsand" statt %USERNAME%, 
:: damit das Wallpaper auch wirklich in SEINEM Profil landet.
set "THEME_DIR=C:\Users\jordsand\AppData\Roaming\Microsoft\Windows\Themes"

:: 4. Ordner erstellen & Kopieren
if not exist "%TARGET_DIR%" mkdir "%TARGET_DIR%"

echo [*] Kopiere Datei...
copy /Y "%SRC%" "%DST%"
if %errorlevel% neq 0 (
    echo [!] FEHLER: Kopiervorgang fehlgeschlagen.
    pause
    goto USER_MGT
)

:: 5. Wallpaper-Cache und Explorer-Reset
echo [*] Bereinige Wallpaper-Cache f r jordsand...
if exist "%THEME_DIR%\TranscodedWallpaper" del /f /q "%THEME_DIR%\TranscodedWallpaper"
if exist "%THEME_DIR%\CachedFiles" del /f /q "%THEME_DIR%\CachedFiles\*.*"

:: Bild als TranscodedWallpaper "tarnen"
copy /Y "%DST%" "%THEME_DIR%\TranscodedWallpaper" >nul

echo [*] F hre Explorer-Reset aus...
taskkill /f /im explorer.exe >nul 2>&1
start explorer.exe

echo [OK] User 'jordsand' bereit und Wallpaper gesetzt.
echo.
pause
goto USER_MGT